package cn.yudao.email;

import cn.yudao.base.BaesTest;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.io.File;

public class email extends BaesTest {

    @Autowired
    private JavaMailSender javaMailSender;
    
    @Test
    public void test() throws Exception{
        // 配置邮件类
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 配置发送邮件者
        simpleMailMessage.setFrom("398991015@qq.com");

        // 配置收到邮件者
        simpleMailMessage.setTo("398991015@qq.com");

        // 配置收到邮件主题
        simpleMailMessage.setSubject("这是测试");
        // 配置邮件发送的内容
        simpleMailMessage.setText("大聪明");

        // 发送邮件
        javaMailSender.send(simpleMailMessage);
    }

    @Test
    public void sendMail() throws Exception{
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 2.发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("398991015@qq.com");
        helper.setSubject("测试复杂邮件");
        // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
        helper.setText("<h1>男德经</h1>"+
                "<img src='http://dfs.java.itsource.cn/group1/M00/00/05/oYYBAGKDLDuADv2ZAABvwky9b5015.jpeg\"' />",true);
        // 4.添加附件
        helper.addAttachment("好东西.jpg",new File("C:\\Users\\colin\\Desktop\\1.jpg"));
        // 5.设置收件人
        helper.setTo("398991015@qq.com");
        // 6.发送邮件
        javaMailSender.send(mimeMessage);
    }
}
